package com.nowcoder.community.controller;

import com.nowcoder.community.entity.Comment;
import com.nowcoder.community.service.CommentService;
import com.nowcoder.community.util.HostHolder;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class commentController {
    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private CommentService commentService;

    @PostMapping("/add/{discussPostId}")  //因为新增评论后需要重定向到当前帖子页面，所以传递路径参数discussPostId
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        //补充comment,包括当前的userId,status,创建时间，因此需要注入Hostholder
        comment.setUserId(hostHolder.getUser().getId());  //有可能用户没登录，发生空指针异常，报错(后面会做统一的异常处理,以及权限的认证，没登录访问不了它)
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        //补充完之后，调用service添加
        commentService.addComment(comment);

        //添加完后，跳回帖子详情页面
        return "redirect:/discuss/detail/" + discussPostId;
    }
}
